package com.shop2cn.iapi.couponmanage.testcase.old;

import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.couponmanage.parameter.CreateBagBean;
import com.shop2cn.iapi.couponmanage.parameter.CreateCouponBean;
import com.shop2cn.iapi.couponmanage.parameter.QuerySendTask;
import com.shop2cn.iapi.couponmanage.parameter.TaskInfo;
import com.shop2cn.iapi.couponmanage.parameter.createTaskByUserIdBean;
import com.shop2cn.iapi.couponmanage.parameter.getTaskBean;
import com.shop2cn.iapi.couponmanage.service.CreateBagCall;
import com.shop2cn.iapi.couponmanage.service.CreateCouponCall;
import com.shop2cn.iapi.couponmanage.service.createTaskByUserIdCall;
import com.shop2cn.iapi.couponmanage.service.getTaskCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CouponShop2cnMCallService;
import com.ymttest.database.model.couponbag;
import com.ymttest.database.model.couponbatch;
import com.ymttest.database.model.couponsendtask;
import com.ymttest.database.sqlwapper.couponbagWapper;
import com.ymttest.database.sqlwapper.couponbatchWapper;
import com.ymttest.database.sqlwapper.couponsendtaskWapper;
import com.ymttest.utils.YMTDateUtil;
/**
*获取任务信息
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_getTask{
	private static getTaskBean gettaskBean;
	private static getTaskCall gettaskCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取任务信息");
	}

	@Before
	public void caseUp() {
		gettaskBean=new getTaskBean();
		gettaskCall=new getTaskCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(gettaskCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Ts_getTask_001() {
		Logger.start(true,"获取任务信息-普通券");
		try {
			CouponShop2cnMCallService couponCallService = new CouponShop2cnMCallService();
			createTaskByUserIdBean createTaskByUserIdBean=couponCallService.setCreateTaskByUserIdBeanForPlatform();
			
			List<Integer> userIds=new ArrayList<Integer>();
			for(int i=1;i<10;i++)
			{
			  userIds.add(i);
			}
			createTaskByUserIdBean.setUserIds(userIds);
			createTaskByUserIdCall taskcall=couponCallService.createTaskByUserId(createTaskByUserIdBean);
			Thread.sleep(2000);  
			gettaskBean.setTaskId(taskcall.getTaskId());
			gettaskBean.setAppId("test");
			gettaskBean.setRequestId("test");
			gettaskCall.setData(gettaskBean);
			gettaskCall.callService();			
			verifyReturn(taskcall.getTaskId());
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Ts_getTask_002() {
		Logger.start(true,"获取任务信息-套券");
		try {
			createTaskByUserIdBean createTaskByUserIdBean=new createTaskByUserIdBean();
			CouponShop2cnMCallService couponCallService = new CouponShop2cnMCallService();
			CreateBagBean createBagBean = couponCallService.setDefaultPlatformCreateCouponBagBeanForRecieve();
			CreateBagCall createBagCall = couponCallService.createPlatformCouponBag(createBagBean);
			
			List<Integer> userIds=new ArrayList<Integer>();
			userIds.add(11);
			createTaskByUserIdBean.setUserIds(userIds);
			createTaskByUserIdBean.setBatchType(2);
			createTaskByUserIdBean.setBatchCode(createBagCall.getbagCode());
			createTaskByUserIdBean.setCreaterId(22);
			createTaskByUserIdBean.setCreaterName("autoTest");
			createTaskByUserIdBean.setNotify(false);
			
			createTaskByUserIdCall taskcall=couponCallService.createTaskByUserId(createTaskByUserIdBean);
			Thread.sleep(2000);  
			gettaskBean.setTaskId(taskcall.getTaskId());
			gettaskBean.setAppId("test");
			gettaskBean.setRequestId("test");
			gettaskCall.setData(gettaskBean);
			gettaskCall.callService();	
			BagverifyReturn(taskcall.getTaskId());
					
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Ts_getTask_003() {
		Logger.start(true,"获取任务信息-TaskId不存在");
		try {
			gettaskBean.setTaskId(9999999);
			gettaskBean.setAppId("test");
			gettaskBean.setRequestId("test");
			gettaskCall.setData(gettaskBean);
			gettaskCall.callService();	
			Logger.verifyEquals("0", gettaskCall.getCode(), "验证返回code");
			Logger.verifyEquals(true, gettaskCall.getMessage().contains("操作成功！"), "验证返回Message");	
			Logger.verifyEquals(null, gettaskCall.getTaskInfo(), "验证返回Message");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Ts_getTask_004() {
		Logger.start(true,"获取任务信息-TaskId为空");
		try {
			gettaskBean.setTaskId(0);
			gettaskBean.setAppId("test");
			gettaskBean.setRequestId("test");
			gettaskCall.setData(gettaskBean);
			gettaskCall.callService();				
			Logger.verifyEquals("1", gettaskCall.getCode(), "验证返回code");
			Logger.verifyEquals(true, gettaskCall.getMessage().contains("taskId:请传入任务ID"), "验证返回Message");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	private void verifyReturn(int TaskId)
	{
		
		couponsendtask cuponSendTask=new couponsendtaskWapper().selectByPrimaryKey(TaskId);
		couponbatch batch = new couponbatchWapper().selectBybatchCode(cuponSendTask.getBatchCode()).get(0);
		Logger.verifyEquals("0", gettaskCall.getCode(), "验证返回code");
		Logger.verifyEquals("操作成功！", gettaskCall.getMessage(), "验证返回Message");
		TaskInfo info = gettaskCall.getTaskInfo();
	
		String createTime=YMTDateUtil.getFormatTime(cuponSendTask.getCreateTime(), "yyyy-MM-dd'T'HH:mm:ss");
		Logger.verifyEquals(cuponSendTask.getTaskId(), info.getTaskId(), "验证返回TaskId");
		Logger.verifyEquals(createTime, info.getCreateTime(), "验证返回CreateTime");
		Logger.verifyEquals(batch.getBatchId(), info.getBatchId(), "验证返回BatchId");
		Logger.verifyEquals(cuponSendTask.getCreaterId(), info.getCreaterId(), "验证返回CreaterId");
		Logger.verifyEquals(cuponSendTask.getCreaterName(), info.getCreaterName(), "验证返回CreaterName");
		Logger.verifyEquals(cuponSendTask.getStatus().intValue(), info.getTaskStatus(), "验证返回Status");
		Logger.verifyEquals(cuponSendTask.getSuccessNum(), info.getCouponSuccessCount(), "验证返回SuccessNum");
		Logger.verifyEquals(cuponSendTask.getFailNum(), info.getCouponFailCount(), "验证返回FailNum");
		Logger.verifyEquals(cuponSendTask.getTotalNum(), info.getCouponTotal(), "验证返回TotalNum");
		Logger.verifyEquals(batch.getBatchCode(), info.getBatchCode(), "验证返回BatchCode");
		Logger.verifyEquals(cuponSendTask.getBatchCode(), info.getBatchCode(), "验证数据库batchCode与返回BatchCode一致");
		Logger.verifyEquals(batch.getCouponName(), info.getBatchName(), "验证返回CouponName");
		Logger.verifyEquals(cuponSendTask.getTaskId(), info.getTaskId(), "验证返回taskId");
	}
	private void BagverifyReturn(int TaskId)
	{
		couponsendtask cuponSendTask=new couponsendtaskWapper().selectByPrimaryKey(TaskId);
		couponbag batch = new couponbagWapper().selectByPrimaryKey(cuponSendTask.getBatchCode());
		Logger.verifyEquals("0", gettaskCall.getCode(), "验证返回code");
		Logger.verifyEquals("操作成功！", gettaskCall.getMessage(), "验证返回Message");
		TaskInfo info = gettaskCall.getTaskInfo();
	
		String createTime=YMTDateUtil.getFormatTime(cuponSendTask.getCreateTime(), "yyyy-MM-dd'T'HH:mm:ss");
		Logger.verifyEquals(cuponSendTask.getTaskId(), info.getTaskId(), "验证返回TaskId");
		Logger.verifyEquals(createTime, info.getCreateTime(), "验证返回CreateTime");
		Logger.verifyEquals(0, info.getBatchId(), "验证返回BatchId");
		Logger.verifyEquals(cuponSendTask.getCreaterId(), info.getCreaterId(), "验证返回CreaterId");
		Logger.verifyEquals(cuponSendTask.getCreaterName(), info.getCreaterName(), "验证返回CreaterName");
		Logger.verifyEquals(cuponSendTask.getStatus().intValue(), info.getTaskStatus(), "验证返回Status");
		Logger.verifyEquals(cuponSendTask.getSuccessNum(), info.getCouponSuccessCount(), "验证返回SuccessNum");
		Logger.verifyEquals(cuponSendTask.getFailNum(), info.getCouponFailCount(), "验证返回FailNum");
		Logger.verifyEquals(cuponSendTask.getTotalNum(), info.getCouponTotal(), "验证返回TotalNum");
		Logger.verifyEquals(batch.getBagCode(), info.getBatchCode(), "验证返回BatchCode");
		Logger.verifyEquals(cuponSendTask.getBatchCode(), info.getBatchCode(), "验证数据库batchCode与返回BatchCode一致");
		Logger.verifyEquals(batch.getName(), info.getBatchName(), "验证返回CouponName");
		Logger.verifyEquals(cuponSendTask.getTaskId(), info.getTaskId(), "验证返回taskId");
	}
}